package com.party.controller.user.inform;

import com.party.common.Page;
import com.party.common.Tools;
import com.party.model.*;
import com.party.service.inform.InformService;
import com.party.service.noticerelation.NoticeRelationService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.*;
import java.util.HashMap;
import java.util.Map;

import com.party.model.UserInfo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created with IntelliJ IDEA.
 * User: beyond
 * Date: 14-10-8
 * Time: 下午10:27
 * To change this template use File | Settings | File Templates.
 */

@Controller
@RequestMapping(value = "/inform")
public class InformController {

    @Resource
    NoticeRelationService noticeRelationService;

    @Resource
    InformService informService;

    private int pageSize = 10;
    /**
     * 首页
     * @return
     */
    @RequestMapping(value = "index")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("user/inform/inform");
        return modelAndView;
    }

    @RequestMapping(value = "/query")
    @ResponseBody
    public Map<String,Object> query(HttpServletRequest req,HttpSession session) {
        Map<String, Object> map = new HashMap<String, Object>();
        String pageIndexStr = req.getParameter("pageIndex");
        String title = req.getParameter("title");
//        String starttime = req.getParameter("starttime");
//        String endtime = req.getParameter("endtime");
        SysNotice sysNotice = new SysNotice();
        if ((SysUser) session.getAttribute("sysUser") != null) {
            Integer uid = ((SysUser) session.getAttribute("sysUser")).getUid();
            sysNotice.setUid(uid);
        }
        int pageIndex = 1;

        if (pageIndexStr != null && !"".equals(pageIndexStr)) {
            pageIndex = Integer.parseInt(pageIndexStr);
        }
        sysNotice.setTitle(title);
//        sysNotice.setStarttime(starttime);
//        sysNotice.setEndtime(endtime);

        Page p = this.informService.getSysNoticeByUser(sysNotice, pageSize, pageIndex);
        map.put("pageCount",p.getTotalPageCount() );
        map.put("tableData",p.getResult());
        return map;
    }

    /**
     * 删除
     *
     * @param ids
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Map<String, Object> delete(
            @RequestParam("ids") String ids,
            HttpSession session,
            HttpServletRequest request,
            HttpServletResponse response
    ) {

        int num = 0;
        try {
            num = informService.deleteExamByIds(ids);

        } catch (Exception e) {
            num = -1;
            e.printStackTrace();
        }
        Map<String, Object> modelMap = new HashMap<String, Object>();
        modelMap.put("num", num);
        return modelMap;
    }

    @RequestMapping(value = "content")
    public ModelAndView content(
            HttpSession session,
            HttpServletRequest request
    ) {
        ModelAndView modelAndView = new ModelAndView();
        SysNotice sysNotice = new SysNotice();
        String idStr = request.getParameter("id");
        if(idStr != null && !"".equals(idStr) && Tools.isNumber(idStr))
        {
            Integer id = Integer.parseInt(idStr);
            sysNotice = this.informService.getNoticeByPrimaryKey(id);
        }
        modelAndView.addObject("obj",sysNotice);
        byte[]binaryContent=sysNotice.getContent();
        String content="";
        if(binaryContent!=null){
            content=new String(binaryContent);
        }

        modelAndView.addObject("content",content);
        modelAndView.setViewName("/user/inform/inform_content");
        return modelAndView;
    }
}

